<?php namespace Common\Model;
use Hdphp\Model\Model;
class sc_goods_list extends Model{
	
	//指定表
	protected $table="sc_goods_list";
	
	private $GoodsAttrModel;
	
	public function __init(){
		$this->GoodsAttrModel=M('\Common\Model\sc_goods_attr');
	}
	
	//自动完成
	protected $auto=[
	    ['inventory','_myinventory','method',3,1]
	
	];
	
	
	//自动验证
	protected $validate=array(
	     array('combine','MYrequired','商品属性必须选择',3,3),
	     array('number','required','库存不能为空',3,3)
	);
	
	//添加商品【货品表】
	public function addGoodsList(){
		if(!$data=$this->create()) return false;
		if(isset($data['combine'])){
			$data['combine']=implode(",", $data['combine']);
		}
		
		$this->add($data); return true;
	}
	
	//通过gid获取本商品[货品表信息]
	public function getGoodsList(){
		$gid=Q('get.gid');
		$data=$this->where('sc_goods_gid',$gid)->get();
		foreach($data as $k=>$v){
			$data[$k]['combine']=$this->GoodsAttrModel
			->where("gtid IN ({$v['combine']})")
			//->field('gtvalue')
			->get();
		}
		return $data;
	}
	
	
	//自动验证，对货品表添加货品的时候，若商品属性有一项为空，则返回失败
	public function MYrequired(){
		foreach(Q('post.combine') as $v){
			if($v==0){
				return false;
			}
		}
		return true;
	}
	
	
	public function _myinventory(){
	    
		return substr(md5(time()), 0,5);
	}	
}
?>